package org.NooLab.nativebrowser.retrieval;

import java.io.Serializable;

import org.NooLab.nativebrowser.components.ItexxModelController;
import org.NooLab.utilities.net.GUID;



/**
 * 
 * request items are internal, technical objects that are bundling keywords and parameters 
 * for issuing a search through a search engine;</br></br>
 * 
 * Such a "RequestItem" is different from the TaskRequest item that is created on behalf of some user input...???
 * 
 * (Note that normal links are just "followed")
 * 
 * 
 * @author kwa
 *
 */
public class RequestItem implements Serializable{

	
	transient private static boolean isSimplePageRetrieval;

	transient RetrievalEngine retrievalEngine;

	String guidStr ="" ;
	
	// we need a group ID for each RequestItem, and the total number items in the group,
	// about which each item need to know!!!
	// -> then we can wait asynchronously for the result, just as we need
	String groupId = "";
	int  groupSize=-1;  // remains < 0 if there is only 1...

	// 
	int countOfNextPages = 0;
	boolean isOK = true ;
	private String requestStr = "";
	String dedicatedFile = "";
	
	
	// ========================================================================
	public RequestItem(){
		guidStr = GUID.randomvalue() ;
	}
	// ========================================================================
	
	
	public void setEngine( RetrievalEngine engine){
		retrievalEngine = engine; 
	}
	
	public String getRequestStr() {
		return requestStr;
	}

	public void setRequestStr(String requestStr) {
		this.requestStr = requestStr;
	}


	public boolean isOk() {
		// 
		return isOK;
	}


	public void perform() {
		//
		
		try{
			
			retrievalEngine.perform(this);
			
			
		}catch(Exception e){
			
		}
	}



	public void setFileContextTemplate(String filename) {
		// 
		dedicatedFile = filename;
		
	}
	
	public static RequestItem prepareRequest( String urlStr){
		RequestItem requestItem;
		requestItem = new RequestItem();

		isSimplePageRetrieval = true;

		requestItem.setRequestStr(urlStr);

		return requestItem;
	}
	
	
	/**
	 */
	public static RequestItem prepareRequestItemDedicated( String rqStr ){ 
		
		RequestItem requestItem;
		requestItem = new RequestItem();

		
		requestItem.setRequestStr(rqStr) ;
		
		
		return requestItem;
	
	}

	/**
	 * 
	 * in case of a direct search request it is pretty clear that the request data is a 
	 * simple text, at least if there was not a file
	 * 
	 * @param rqStr
	 * @param itexxModel
	 * @return
	 */
	public static RequestItem prepareRequestItemDedicated( String rqStr, 
			  									  		   ItexxModelController itexxModel){ 
		
		return prepareRequestItem( rqStr, "",itexxModel);
	}

	/**
	 * 
	 * 
	 * @param rqStr
	 * @param filename
	 * @param itexxModel
	 * @return
	 */
	public static RequestItem prepareRequestItem( String rqStr, 
												   String filename,
												   ItexxModelController itexxModel){ 
												// RequestFocusContext focus, boolean fullrequest, boolean filecontext) {
		// .requestFocusContext, fullRequest,fileContext
		
		RequestItem requestItem;
		requestItem = new RequestItem();
		

		if (itexxModel.isFileContext() ){
			// this is NOT the background as given in "focus", it is a dedicated search for the content in that file
			requestItem.setFileContextTemplate(filename);
		}
		requestItem.setRequestStr(rqStr) ;
		
		
		return requestItem;
	}


	public String getDedicatedFile() {
		return dedicatedFile;
	}


	public void setDedicatedFile(String dedicatedFile) {
		this.dedicatedFile = dedicatedFile;
	}


	public int getCountOfNextPages() {
		return countOfNextPages;
	}


	public void setCountOfNextPages(int countOfNextPages) {
		this.countOfNextPages = countOfNextPages;
	}


	public String getGuidStr() {
		return guidStr;
	}


	public void setGuidStr(String guidStr) {
		this.guidStr = guidStr;
	}


	public String getGroupId() {
		return groupId;
	}


	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}


	public int getGroupSize() {
		return groupSize;
	}


	public void setGroupSize(int groupSize) {
		this.groupSize = groupSize;
	}


	public boolean isOK() {
		return isOK;
	}


	public void setOK(boolean isOK) {
		this.isOK = isOK;
	}
	
	
	
}
